package when3meet.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;

import when3meet.controller.CreateEventBtnController;
import when3meet.controller.SignInController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;

public class SignInFrame extends JFrame {

	JPanel contentPane;
	JTextField userNameField;
	JTextField eventIDField;
	static SignInFrame frame;
	private JPasswordField passwordField;
	JButton signInBtn;
	JCheckBox isAdmin;

	/**
	 * returen instance of sign in frame
	 */
	public static SignInFrame getInstance() {

		if (frame == null) {
			frame = new SignInFrame();
		} else {

		}
		return frame;
	}

	/**
	 * Create the frame.
	 */
	private SignInFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setLocationRelativeTo(null);

		JLabel lblUserName = new JLabel("User Name");

		JLabel lblDecisionLine = new JLabel("Decision Line ");
		lblDecisionLine.setHorizontalAlignment(SwingConstants.CENTER);
		lblDecisionLine.setFont(new Font("Tahoma", Font.BOLD, 17));

		userNameField = new JTextField();
		userNameField.setColumns(10);

		JLabel lblNewLabel = new JLabel("Password");

		JLabel lblEventId = new JLabel("Event ID");

		eventIDField = new JTextField();
		eventIDField.setColumns(10);

		JButton createEventBtn = new JButton("Create Event");
		// bind action to create event button
		createEventBtn.addActionListener(new CreateEventBtnController(this));

		signInBtn = new JButton("Sign In");

		// bind action to sign in button
		signInBtn.addActionListener(new SignInController(this));

		signInBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});

		passwordField = new JPasswordField();

		isAdmin = new JCheckBox("Admin");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						gl_contentPane
								.createSequentialGroup()
								.addGroup(
										gl_contentPane
												.createParallelGroup(Alignment.LEADING)
												.addGroup(
														gl_contentPane
																.createSequentialGroup()
																.addGap(54)
																.addGroup(
																		gl_contentPane
																				.createParallelGroup(Alignment.LEADING)
																				.addComponent(lblUserName)
																				.addComponent(lblNewLabel)
																				.addComponent(lblEventId)))
												.addGroup(
														Alignment.TRAILING,
														gl_contentPane
																.createSequentialGroup()
																.addGap(51)
																.addComponent(isAdmin, GroupLayout.DEFAULT_SIZE, 55,
																		Short.MAX_VALUE)))
								.addGap(63)
								.addGroup(
										gl_contentPane
												.createParallelGroup(Alignment.TRAILING)
												.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 193,
														Short.MAX_VALUE)
												.addComponent(userNameField, GroupLayout.DEFAULT_SIZE, 193,
														Short.MAX_VALUE)
												.addGroup(
														gl_contentPane
																.createSequentialGroup()
																.addComponent(createEventBtn, GroupLayout.DEFAULT_SIZE,
																		GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																.addGap(18)
																.addComponent(signInBtn, GroupLayout.PREFERRED_SIZE,
																		78, GroupLayout.PREFERRED_SIZE))
												.addComponent(eventIDField, GroupLayout.DEFAULT_SIZE, 193,
														Short.MAX_VALUE)).addGap(62))
				.addGroup(
						gl_contentPane.createSequentialGroup().addGap(155)
								.addComponent(lblDecisionLine, GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
								.addGap(150)));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(
				gl_contentPane
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblDecisionLine)
						.addGap(32)
						.addGroup(
								gl_contentPane
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(eventIDField, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblEventId))
						.addGap(18)
						.addGroup(
								gl_contentPane
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(userNameField, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblUserName))
						.addGap(23)
						.addGroup(
								gl_contentPane
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(passwordField, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblNewLabel))
						.addGap(32)
						.addGroup(
								gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(signInBtn)
										.addComponent(createEventBtn).addComponent(isAdmin))
						.addContainerGap(31, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
	}

	public JTextField getUserNameField() {
		return userNameField;
	}

	public JTextField getEventIDField() {
		return eventIDField;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public JButton getSignInButton() {
		return signInBtn;
	}

	public JCheckBox getIsAdmin() {
		return isAdmin;
	}

}
